﻿using Hotel.Model.House;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotel.DAL
{
    public class HouseDAL
    {
        //注入数据库的上下文
        MyDBContext db;
        public HouseDAL(MyDBContext db)
        { 
            this.db = db; 
        }

        /// <summary>
        /// 房型的增加
        /// </summary>
        /// <param name="info">房型的实体</param>
        /// <returns>影响的行数</returns>
        public int PostHouseTypeAdd(T_House_Type info)
        {
            try
            {
                db.T_House_Type.Add(info);
                return db.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 查房型是否存在
        /// </summary>
        /// <param name="houseTypeName">房型</param>
        /// <returns>返回符合条件的数据</returns>
        public int SearchHouseTypeName(string  houseTypeName) {
            try
            {
                int res = db.T_House_Type.Where(c=>c.HY_Name == houseTypeName).Count();
                return res; 
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取所有的房型
        /// </summary>
        /// <param name="totalPage">总页数</param>
        /// <param name="totalCount">总条数</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>返回符合条件的数据</returns>
        public List<T_House_Type> GetHouseTypeList(out int totalPage,out int totalCount,int pageIndex = 1,int pageSize=2) {
            try
            {
                //获取所有显示的数据
                List<T_House_Type>  list = db.T_House_Type.Where(c=>c.IsDelete == false).ToList();

                //分页
                //总条数
                totalCount = list.Count() ;
                //总页数
                totalPage   = (int)Math.Ceiling(totalCount * 1.0 / pageSize) ;

                list = list.OrderBy(c=>c.HY_Id).Skip((pageIndex-1)*pageSize).Take(pageSize).ToList() ;

                return list;

            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 根据id获取数据  实现数据的反填
        /// </summary>
        /// <param name="HtId">房型id</param>
        /// <returns></returns>
        public T_House_Type GetHouseTypeById(int HtId)
        {
            try
            {
                // T_House_Type  info = db.T_House_Type.Where(c=>c.HY_Id == HtId).FirstOrDefault();
                T_House_Type info = db.T_House_Type.Find(HtId);
                return info;
            }
            catch (Exception)
            {

                throw;
            }
        }

      /// <summary>
      /// 确认修改
      /// </summary>
      /// <param name="info">房型的实体</param>
      /// <returns>影响的行数</returns>
        public int PutHouseTypeById(T_House_Type info)
        {
            try
            {
                //1.先根据id找到旧的实体
                var oldInfo = db.T_House_Type.Find(info.HY_Id);
                //2.进行修改
                oldInfo.HY_Name = info.HY_Name;
                oldInfo.HY_Price = info.HY_Price;
                oldInfo.HY_Photo = info.HY_Photo;
                oldInfo.HY_Window = info.HY_Window;
                oldInfo.HY_Number = info.HY_Number;
                oldInfo.HY_Area = info.HY_Area;
                oldInfo.HY_Desc = info.HY_Desc;
                oldInfo.HY_UpdateTime =  DateTime.Now;
                //3.更新一下数据库
                return db.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
       
        /// <summary>
        /// 根据id修改房型状态
        /// </summary>
        /// <param name="HTid"></param>
        /// <param name="HTstate"></param>
        /// <returns></returns>
        public int PutHouseTypeStateById(int HTid,int HTstate)
        {
            try
            {
                //根据id找到这一条对记录
                var info = db.T_House_Type.Find(HTid);
                //修改状态
                info.HY_State = HTstate;
                //更新一下数据库
                return db.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 根据id进行删除一条记录
        /// </summary>
        /// <param name="HTid">房型id</param>
        /// <returns></returns>

        public int DeleteHouseTypeById(int HTid)
        {
            try
            {
                var info = db.T_House_Type.Find(HTid);
                info.IsDelete = true;
                return db.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 实现批量删除
        /// </summary>
        /// <param name="idsArr"></param>
        /// <returns></returns>
        public int DeleteAllHouseTypeById(string idsArr)
        {
            try
            {
                //转数组  
                var arr = idsArr.Split(',');  //1,2,3
                foreach (string id in arr)
                {
                    var info = db.T_House_Type.Find(Convert.ToInt32(id));
                    info.IsDelete = true;
                }
                //更新数据库
                return db.SaveChanges();

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取房态
        /// </summary>
        /// <returns>返回符合条件的数据</returns>
        public List<T_House_State> GetHouseStateList()
        {
            try
            {
                List<T_House_State> list = db.T_House_State.ToList();
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 增加房间号
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int PostAddHouseNo(T_House_No info)
        {
            try
            {
                 db.T_House_No.Add(info);
                return db.SaveChanges();    
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 根据房间号和门查询是否重复,
        /// </summary>
        /// <param name="houseNo">房间号</param>
        /// <returns></returns>
        public int SeachHouseNo(string houseNo,int storeId)
        {
            try
            {
                return db.T_House_No.Where(c => c.HN_Name == houseNo && c.StoreId == storeId).Count();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 房间号分页
        /// </summary>
        /// <param name="totalPage">总页数</param>
        /// <param name="totalCount">总条数</param>
        /// <param name="houseNo">房间号</param>
        /// <param name="htId">房型</param>
        /// <param name="storeId">门店</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns></returns>
        public List<newHouseNo> GetHouseNoInfos(out int totalPage,out int totalCount,string? houseNo="", int? htId = null,int? storeId=null,int pageIndex=1,int pageSize=2)
        {
            try
            {
                //1.查询所有的房间
                var list = db.T_House_No.Join(db.T_House_Type, a => a.HY_Id, b => b.HY_Id, (a, b) => new newHouseNo
                {
                    HN_Name = a.HN_Name,
                    HN_Id = a.HN_Id,
                    StoreId = a.StoreId,
                    IsDeleted = a.IsDeleted,
                    HS_Id = a.HS_Id,
                    HY_Photo= b.HY_Photo,
                    HY_Name =  b.HY_Name,
                    HY_Price =  b.HY_Price,
                    HY_Id =  b.HY_Id,
                }).Join(db.T_House_State, a => a.HS_Id, b => b.HS_Id, (a, b) => new newHouseNo
                {
                    HN_Id = a.HN_Id,
                    HN_Name = a.HN_Name,
                    StoreId=a.StoreId,
                    HS_Id= a.HS_Id,
                    HY_Photo =a.HY_Photo,
                    HY_Name =  a.HY_Name,
                    HY_Price = a.HY_Price,
                    IsDeleted =   a.IsDeleted,
                    HY_Id = a.HY_Id,
                    HS_Name = b.HS_Name,
                }).Where(c=>c.IsDeleted == false).ToList();

                //根据房型
                if(htId != null)
                {
                    list = list.Where(c => c.HY_Id == htId).ToList();
                }

                //根据门店
                if (storeId != null)
                {
                    list = list.Where(c => c.StoreId == storeId).ToList();
                }

                //根据房间号
                if (!string.IsNullOrEmpty(houseNo))
                {
                    list = list.Where(c => c.HN_Name == houseNo).ToList();
                }

                //分页

                //总条数
                totalCount = list.Count();

                //总页数
                totalPage = (int)Math.Ceiling(totalCount * 1.0 / pageSize);

                //分页
                list = list.OrderBy(c => c.HN_Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

                return list;

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取所有的空闲房间
        /// </summary>
        /// <returns></returns>
        public IQueryable GetHouseState(int? htId = null,int? storeId = null)
        {
            try
            {
               //1.先查出所有的空闲的房子
                var list =  db.T_House_No.Join(db.T_House_Type, a => a.HY_Id, b => b.HY_Id, (a, b) => new
                {
                    a.HN_Id,
                    a.StoreId,
                    a.HS_Id,
                    a.HN_Name,
                    b.HY_Name,
                    b.HY_Id,

                }).Join(db.T_House_State, a => a.HS_Id, b => b.HS_Id, (a, b) => new
                {
                    a.HN_Id,
                    a.HS_Id,
                    a.StoreId,
                    a.HN_Name,
                    a.HY_Name,
                    a.HY_Id,
                    b.HS_Name,
                }).
                Join(db.T_Store, a => a.StoreId, b => b.StoreId,(a,b)=> new {
                    a.HN_Id,
                    a.HS_Id,
                    a.StoreId,
                    a.HN_Name,
                    a.HY_Name,
                    a.HS_Name,
                    a.HY_Id,
                    b.StoreName,
                }).
                Where(c => c.HS_Id == 1);

                //根据房型
                if (htId != null)
                {
                    list = list.Where(c=>c.HY_Id == htId);
                }


                //根据门店查
                if (storeId != null)
                {
                    list = list.Where(c => c.StoreId == storeId);
                }

                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 找某个门店下的房间变成已入住就行
        /// </summary>
        /// <param name="hsId"></param>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public int GetHouseNoStateStore(int hsId,int storeId)
        {
            try
            {
                var info = db.T_House_No.Where(c => c.HN_Id == hsId && c.StoreId == storeId).FirstOrDefault();
                info.HS_Id = 2;
                return db.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 增加房价计划
        /// </summary>
        /// <param name="info">房价计划实体类</param>
        /// <returns>返回影响的行数</returns>
        public int PostAddHousePrice(T_House_Price info)
        {
            try
            {
                db.T_House_Price.Add(info);
                return db.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 某种房型下的房价计划名称不能重复
        /// </summary>
        /// <param name="name"></param>
        /// <param name="htId"></param>
        /// <returns></returns>
        public int SearchHousePriceName(string name,int htId)
        {
            try
            {
                int res = db.T_House_Price.Where(c=>c.PH_Name == name && c.HY_Id ==htId).Count();
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据房型、房价计划名称  开始时间和结束时间  分页
        /// </summary>
        /// <returns></returns>
        public  IQueryable GetHousePriceList()
        {
            try
            {
                var list = db.T_House_Price.Join(db.T_House_Type, a => a.HY_Id, b => b.HY_Id, (a, b) => new
                {
                    a.HP_Id,
                    a.PH_Name,
                    a.PH_BeginTime,
                    a.PH_EndTime,
                    a.PH_Number,
                    a.StoreId,
                    b.HY_Name
                });
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 房间删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteHouseNo(int id)
        {
            try
            {
                var info = db.T_House_No.Find(id);
                info.IsDeleted = true;
               return db.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 修改房间的状态
        /// </summary>
        /// <param name="houseId"></param>
        /// <param name="houseStateId"></param>
        /// <returns></returns>
        public int PutHouseNoState(int houseId,int houseStateId)
        {
            try
            {
                var info = db.T_House_No.Find(houseId);
                info.HS_Id = houseStateId;
                return db.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
